package www.bplead.com.mydemo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import www.bplead.com.mydemo.R;

/**
 * Created by wangyangzi on 2016/9/26.
 */
public class CustomImageView extends View{
    //标题
    private String titleText;
    //字体大小
    private int titleSize;
    //字体颜色
    private int titleColor;
    //图片
    private Bitmap mImage;
    //缩放类型
    private int scaleType;

    static final int IMAGE_SCALE_FITXY = 0;
    static final int IMAGE_SCALE_CENTER = 1;

    //View的宽度
    private int mWidth;
    //View的高度
    private int mHeight;

    private Paint mPaint;
    //文本矩形
    private Rect textRect;
    //图片矩形
    private Rect imageRect;


    public CustomImageView(Context context) {
        this(context,null);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获得自定义属性
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView,defStyleAttr,0);
        for(int i=0;i<a.getIndexCount();i++){
             int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.CustomImageView_civTitleText:
                    titleText = a.getString(attr);
                    break;
                case R.styleable.CustomImageView_civTitleTextSize:
                    // 默认设置为16sp，TypeValue也可以把sp转化为px
                    titleSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomImageView_civTitleTextColor:
                    // 默认颜色设置为黑色
                    titleColor = a.getColor(attr,Color.BLACK);
                    break;
                case R.styleable.CustomImageView_civImage:
                    mImage = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));
                    break;
                case R.styleable.CustomImageView_civImageScaleType:
                    scaleType = a.getInt(attr,0);
                    break;
            }
        }
        //回收资源
        a.recycle();
        mPaint = new Paint();
        //实例化文本和图片矩形
        textRect = new Rect();
        imageRect = new Rect();
        //设置字体大小
        mPaint.setTextSize(titleSize);
        //获取文本绘制范围
        mPaint.getTextBounds(titleText,0,titleText.length(),textRect);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //设置宽度
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        //layout_width -> MatchParent
        if(specMode == MeasureSpec.EXACTLY){
            mWidth = specSize;
        }
        //layout_width -> WrapParent
        else{
            //由图片决定的宽
            int widthByImg = getPaddingLeft() + getPaddingRight() + imageRect.width();
            //由字体决定的宽
            int widthByText = getPaddingLeft() + getPaddingRight() + textRect.width();

            if(specMode == MeasureSpec.AT_MOST){
                int width = Math.max(widthByImg,widthByText);
                mWidth = Math.min(width,specSize);
            }
        }

        //设置高度
        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);
        //layout_height -> MatchParent
        if(specMode == MeasureSpec.EXACTLY){
            mHeight = specSize;
        }
        //layout_height -> WrapParent
        else{
            //由图片决定的高
            int heightByImg = getPaddingTop() + getPaddingBottom() + imageRect.height();
            //由字体决定的高
            int heightByText = getPaddingTop() + getPaddingBottom() + textRect.height();

            if(specMode == MeasureSpec.AT_MOST){
                int height = Math.max(heightByImg,heightByText);
                mHeight = Math.min(height,specSize);
            }
        }
        setMeasuredDimension(mWidth,mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        //绘制边框
        mPaint.setStrokeWidth(4);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.CYAN);
        canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);

        imageRect.left = getPaddingLeft();
        imageRect.right = mWidth - getPaddingRight();
        imageRect.top = getPaddingTop();
        imageRect.bottom = mHeight - getPaddingBottom();

        //绘制文本
        mPaint.setColor(titleColor);
        mPaint.setStyle(Paint.Style.FILL);
        if(textRect.width() > mWidth){//当文本长度大于边框使用...代替
            TextPaint paint = new TextPaint(mPaint);
            String msg = TextUtils.ellipsize(titleText, paint, (float) mWidth - getPaddingLeft() - getPaddingRight(),
                    TextUtils.TruncateAt.END).toString();
            canvas.drawText(msg, getPaddingLeft(), mHeight - getPaddingBottom(), mPaint);
        }
        //正常情况，将字体居中
        else{
            canvas.drawText(titleText, mWidth / 2 - textRect.width() * 1.0f / 2, mHeight - getPaddingBottom(), mPaint);
        }

        //取消使用掉的快
        imageRect.bottom -= textRect.height();

        //绘制图片
        if (scaleType == IMAGE_SCALE_FITXY)
        {

            canvas.drawBitmap(mImage, null, imageRect, mPaint);
        }
        else
        {
            //计算居中的矩形范围
            imageRect.left = mWidth / 2 - mImage.getWidth() / 2;
            imageRect.right = mWidth / 2 + mImage.getWidth() / 2;
            imageRect.top = (mHeight - textRect.height()) / 2 - mImage.getHeight() / 2;
            imageRect.bottom = (mHeight - textRect.height()) / 2 + mImage.getHeight() / 2;

            canvas.drawBitmap(mImage, null, imageRect, mPaint);
        }
    }
}
